#include "ConfigTab.h"

#ifndef ROOT_TGLabel
#include <TGLabel.h>
#endif

#include <TGraph.h>
#include <TGButtonGroup.h>
#include <root/TGFileDialog.h>

ConfigTab::ConfigTab(void)
{
    m_useChanel.resize(5);
}


ConfigTab::~ConfigTab(void)
{
}

void ConfigTab::Create( TGTab *fDataTabCalibTab, void * appPtr )
{
     // container of "Tab2"
    TGCompositeFrame *fCompositeFrame979 = fDataTabCalibTab->AddTab("Configuration tab");
    fCompositeFrame979->SetLayoutManager(new TGVerticalLayout(fCompositeFrame979));
    fCompositeFrame979->SetLayoutBroken(kTRUE);

    t_fCompositeFrame979 = fCompositeFrame979;

    TGButtonGroup *syncGroup = new TGButtonGroup(fCompositeFrame979,"Measurement synchronization",kVerticalFrame);
    m_sync = new TGRadioButton(syncGroup,new TGHotString("&Synchronized"), SYNC_ID);
    TGRadioButton *noSync = new TGRadioButton(syncGroup,new TGHotString("&Not syncronized"), SYNC_ID+1);
    noSync->SetState(kButtonDown);
    syncGroup->Connect("Clicked(Int_t)", "ConfigTab", (void*)this, "SyncChanged(Int_t)");
    syncGroup->MoveResize(16,24,32,18);
    syncGroup->Show();

    TGButtonGroup *chanelGroup = new TGButtonGroup(fCompositeFrame979,"Chanel selection mode",kVerticalFrame);
    m_autoChoose = new TGRadioButton(chanelGroup,new TGHotString("&Automatically select chanels"), CHOOSE_ID);
    m_autoChoose->SetEnabled(false);
    m_manualChoose = new TGRadioButton(chanelGroup,new TGHotString("&Manually select chanels"), CHOOSE_ID+1);
    m_manualChoose->SetState(kButtonDown);
    chanelGroup->Connect("Clicked(Int_t)", "ConfigTab", (void*)this, "ChoiseModeChanged(Int_t)");
    chanelGroup->MoveResize(16,96,128,18);
    chanelGroup->Show();

    int step = 24;
    int yPos = 160;
    char name[256];
    for(int i = 0; i < m_useChanel.size(); ++i)
    {
       sprintf( name, "Chanel %d\0", i );
       m_useChanel[i] = new TGCheckButton(fCompositeFrame979, name);
       m_useChanel[i]->SetState(kButtonDown);
       //chanelGroup->AddFrame(m_useChanel[i], new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
       m_useChanel[i]->MoveResize(48, yPos, 106, 19);

       yPos += step;
    } 

    TGButtonGroup *calibOptions = new TGButtonGroup(fCompositeFrame979,"Calibration options",kVerticalFrame);
    m_useBgCalib = new TGCheckButton(calibOptions,"Use background callibration");
    m_useStatWeight = new TGCheckButton(calibOptions,"Use statistical weight");
    m_useStatWeight->SetState(kButtonDown);
    m_useFilers = new TGCheckButton(calibOptions,"Filter signals");
    m_useFilers->SetState(kButtonDown);
    calibOptions->Move(16,280);
    calibOptions->Show();

    // "fGroupFrame3062" group frame
    TGButtonGroup *addOutput = new TGButtonGroup(fCompositeFrame979,"Additional output",kVerticalFrame);

    m_drawSignals = new TGCheckButton(addOutput,"Draw signals");
    m_printIntegrals = new TGCheckButton(addOutput,"Print integras");
    m_showRawResults = new TGCheckButton(addOutput,"Show results without correction");
    addOutput->Move(16,364);
    addOutput->Show();

    // "fGroupFrame1113" group frame
    TGGroupFrame *calibGroup = new TGGroupFrame(fCompositeFrame979,"Calibrations");
    calibGroup->SetLayoutBroken(kTRUE);

    // "Fon Callibration" group frame
    TGGroupFrame *bgCalibGroup = new TGGroupFrame(calibGroup,"Background noise Callibration");
    bgCalibGroup->SetLayoutBroken(kTRUE);

    // Due to the sad fact that ROOT 5.13 does not support \n symbol in TGLabel
    // thow labels are used here for two lines of text
    // You, my successor are the one to make them update their ROOT
    // Find courage in your heart and the reward will be great
    TGLabel *bgCalibShotNumLabel_1 = new TGLabel(bgCalibGroup,"Calibration shot numbers");
    TGLabel *bgCalibShotNumLabel_2 = new TGLabel(bgCalibGroup,"(separated with a semicolumn)");
    bgCalibShotNumLabel_1->SetTextJustify(100);
    bgCalibShotNumLabel_2->SetTextJustify(100);
    bgCalibGroup->AddFrame(bgCalibShotNumLabel_1, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
    bgCalibGroup->AddFrame(bgCalibShotNumLabel_2, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
    bgCalibShotNumLabel_1->MoveResize(64,24,184,16);
    bgCalibShotNumLabel_2->MoveResize(64,40,184,16);

    m_bgShots = new TGTextEdit(bgCalibGroup);
//    m_bgShots->SetText("");
    bgCalibGroup->AddFrame(m_bgShots, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
    m_bgShots->MoveResize(16,64,288,100);

    TGTextButton *calibrateBg = new TGTextButton(bgCalibGroup,"Calibrate");
    calibrateBg->SetTextJustify(36);
    bgCalibGroup->AddFrame(calibrateBg, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
    calibrateBg->MoveResize(88,170,144,24);
    calibrateBg->Connect("Pressed()", "AppGUI", appPtr, "RunCalibrateBg()");

    bgCalibGroup->SetLayoutManager(new TGVerticalLayout(bgCalibGroup));
    calibGroup->AddFrame(bgCalibGroup, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
    bgCalibGroup->MoveResize(10,24,334,216);


    // "Peak ratio Callibration" group frame
    TGGroupFrame *peakRatioCalibGroup = new TGGroupFrame(calibGroup,"Peak ratio Callibration");
    peakRatioCalibGroup->SetLayoutBroken(kTRUE);

    m_normToSecPeak = new TGCheckButton(peakRatioCalibGroup,"Normalize to second peak");
    peakRatioCalibGroup->AddFrame(m_normToSecPeak, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
    m_normToSecPeak->MoveResize(8,16,256,24);
    m_normToSecPeak->Connect("Clicked()", "ConfigTab", this, "NormToSecPeak()");

    TGLabel *fLabel1006 = new TGLabel(peakRatioCalibGroup,"Archive name");
    peakRatioCalibGroup->AddFrame(fLabel1006, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
    fLabel1006->MoveResize(8,48,112,18);

    m_peakArc = new TGTextEntry(peakRatioCalibGroup, new TGTextBuffer(15));
    m_peakArc->SetMaxLength(255);
    m_peakArc->SetAlignment(kTextLeft);
    m_peakArc->SetText("");
    peakRatioCalibGroup->AddFrame(m_peakArc, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
    m_peakArc->MoveResize(8,70,256,22);

    m_browse = new TGTextButton(peakRatioCalibGroup,"Browse...");
    peakRatioCalibGroup->AddFrame(m_browse, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
    m_browse->MoveResize(270,70,70,24);
    m_browse->Connect("Pressed()", "ConfigTab", this, "BrowseArc()");

    // The same sad story as with bg<CalibShotNumLabel_*...
    TGLabel *peakCalibShotNumsLabel_1 = new TGLabel(peakRatioCalibGroup,"Calbration shot numbers");
    TGLabel *peakCalibShotNumsLabel_2 = new TGLabel(peakRatioCalibGroup,"(separated with a semicolumn)");
    peakCalibShotNumsLabel_1->SetTextJustify(36);
    peakCalibShotNumsLabel_2->SetTextJustify(36);
    peakRatioCalibGroup->AddFrame(peakCalibShotNumsLabel_1, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
    peakRatioCalibGroup->AddFrame(peakCalibShotNumsLabel_2, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
    peakCalibShotNumsLabel_1->MoveResize(64,100,181,16);
    peakCalibShotNumsLabel_2->MoveResize(64,120,181,16);

    m_peakShots = new TGTextEdit(peakRatioCalibGroup);
    TGText *text = new TGText("10");
    for( int i = 8; i >= 0; --i )
    {
        char ch[256];
        sprintf(ch, "%d", i+1);
        text->InsLine(0, ch);
    }

    m_peakShots->SetText(text);
    peakRatioCalibGroup->AddFrame(m_peakShots, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
    m_peakShots->MoveResize(8,140,312,100);

    TGButtonGroup *peakCalibOpt = new TGButtonGroup(peakRatioCalibGroup,"",kVerticalFrame);

    m_normToCustomChanel = new TGRadioButton(peakCalibOpt,"Normalize to custom chanel");
    m_normToCustomChanel->SetState(kButtonDown);
    m_normToSum = new TGRadioButton(peakCalibOpt,"Normalize to sum");
    m_additionalNorm = new TGRadioButton(peakCalibOpt,"Additional norm"); 

    peakCalibOpt->SetBorderDrawn(false);
    peakCalibOpt->Move(8, 240);
    peakCalibOpt->Show();

    m_calibratePeak = new TGTextButton(peakRatioCalibGroup,"Calibrate");
    m_calibratePeak->SetTextJustify(36);
    peakRatioCalibGroup->AddFrame(m_calibratePeak, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
    m_calibratePeak->MoveResize(96,320,144,24);
    m_calibratePeak->Connect("Pressed()", "AppGUI", appPtr, "RunCalibratePeak()");

    peakRatioCalibGroup->SetLayoutManager(new TGVerticalLayout(peakRatioCalibGroup));
    calibGroup->AddFrame(peakRatioCalibGroup, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
    peakRatioCalibGroup->MoveResize(370,24,360,400);

    calibGroup->SetLayoutManager(new TGVerticalLayout(calibGroup));
    fCompositeFrame979->AddFrame(calibGroup, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
    calibGroup->MoveResize(276,24,750,532);
}

std::vector<bool> ConfigTab::GetChanelStates()
{
    std::vector<bool> res;

    if( Selected(m_manualChoose) )
    {
        for( int i = 0; i < m_useChanel.size(); ++i )
            res.push_back(Selected(m_useChanel[i]));
    }
    else
    {
        res.resize(m_useChanel.size(), true);
    }
    
    return res;
}

void ConfigTab::NormToSecPeak()
{
    if( m_normToSecPeak->GetState() == kButtonDown )
    {
        m_peakArc->SetEnabled(false);
        m_peakShots->SetReadOnly(true);
        m_normToCustomChanel->SetEnabled(false);
        m_normToSum->SetEnabled(false);
        m_additionalNorm->SetEnabled(false);
        m_browse->SetEnabled(false);
        m_calibratePeak->SetEnabled(false);
    }
    else
    {
        m_peakArc->SetEnabled(true);
        m_peakShots->SetReadOnly(false);
        m_normToCustomChanel->SetEnabled(true);
        m_normToSum->SetEnabled(true);
        m_additionalNorm->SetEnabled(true);
        m_browse->SetEnabled(true);
        m_calibratePeak->SetEnabled(true);
    }
}

calib_options ConfigTab::GetCalibOpts(calib_type type)
{
    if( type == peak_calib )
    {
        if( m_normToCustomChanel->GetState() == kButtonDown )
            return peak_normCustomCh;
        else if( m_normToSum->GetState() == kButtonDown )
            return peak_normSum;
        else if( m_additionalNorm->GetState() == kButtonDown )
            return peak_additionalNorm;
        else if( m_normToSecPeak->GetState() == kButtonDown )
            return peak_normSecond;
    }
    else
        return opt_none;
};

std::vector<std::string> ConfigTab::GetShots(calib_type type)
{
    std::vector<std::string > lines;
    TGText *text = NULL;

    if( type == bg_calib )
        text = m_bgShots->GetText();
    else if( type == peak_calib )
        text = m_peakShots->GetText();
    else
        return lines;

    char *str = NULL;
    for(int i = 0; i < text->RowCount(); ++i)
    {
        str = text->GetLine(TGLongPosition(0,i), 100);

        if(str != NULL)
        {
            lines.push_back( std::string(str) );
            
            delete str;
            str = NULL;
        }
    }

    return lines;
};

void ConfigTab::BrowseArc()
{
    TGFileInfo fi;
    fi.fMultipleSelection = false;
    new TGFileDialog(NULL, t_fCompositeFrame979, kFDOpen, &fi);
    if( fi.fFilename != NULL )
        m_peakArc->SetText(fi.fFilename);
};

std::string ConfigTab::GetArcName()
{
    TString name = m_peakArc->GetDisplayText();
    return std::string(name.Data());
}